﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartSpace.Core;

namespace SmartSpace.OA
{
    public partial class OAService : IOAService
    {
        public ServiceResult SaveWorkLog(WorkLog log)
        {
            using (OADbContext context = new OADbContext())
            {
                EntityRepository.Save(log, context);

                return new ServiceResult(EntityRepository.Commit(context));
            }
        }

        public PagedList<WorkLog> GetWorkLogs(string account, int start, int limit)
        {
            using (OADbContext context = new OADbContext())
            {
                List<WorkLog> list = context.WorkLogSet.Where(p => p.Account == account)
                    .OrderBy(p => p.LogTime)
                    .Skip(start)
                    .Take(limit)
                    .ToList();
                return new PagedList<WorkLog>(list, start, limit, list.Count);
            }
        }

        public WorkLog GetWorkLog(Guid id)
        {
            using (OADbContext context = new OADbContext())
            {
                return context.WorkLogSet.Where(p => p.Id == id).Single();
            }
        }

        public List<DateTime> GetWorkLogs(string account, int year)
        {
            using (OADbContext context = new OADbContext())
            {
                DateTime beginDate = new DateTime(year, 1, 1);
                DateTime endDate = new DateTime(year + 1, 1, 1);
                return context.WorkLogSet.Where(p => p.Account == account && p.LogDate >beginDate  && p.LogDate < endDate)
                    .OrderBy(p => p.LogDate)
                    .Select(p=>p.LogDate)
                    .Distinct()
                    .ToList();
            }
        }

        public List<WorkLog> GetWorkLogs(string account, DateTime date)
        {
            using (OADbContext context = new OADbContext())
            {
                return context.WorkLogSet.Where(p => p.Account == account && p.LogDate == date.Date)
                    .OrderBy(p => p.LogTime)
                    .ToList();
            }
        }
    }
}
